<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Category;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class CategoryController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Category(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
//            $grid->column('parent_id');
            $grid->column('level', '层级');
//            $grid->column('is_directory')->display(function ($value) {
//                return $value ? '是' : '否';
//            });
            // $grid->column('path', '类目路径');
            $grid->column('full_name', '类目路径')->display(function ($value) {
                return $this->full_name ?? '';
            });

            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                // $filter->equal('id');
                $filter->like('title');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param  mixed  $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Category(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('parent_id');
            $show->field('is_directory');
            $show->field('level');
            $show->field('path');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $menuModel = \App\Models\Category::class;

        return Form::make(new Category(), function (Form $form) use ($menuModel) {
            $form->display('id');
            $form->text('title');
            // $form->select('parent_id', '父类目')->ajax('/admin/api/categories');
            $form->select('parent_id', '父级类目')->options(function () use ($menuModel) {
                return $menuModel::selectOptions();
            })->saving(function ($v) {
                return (int) $v;
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    // 定义下拉框搜索接口
    public function apiIndex(Request $request)
    {
        // 用户输入的值通过 q 参数获取
        $search = $request->input('q');
        $result = \App\Models\Category::query()
            ->where('is_directory', true)  // 由于这里选择的是父类目，因此需要限定 is_directory 为 true
            ->where('title', 'like', '%' . $search . '%')
            ->paginate();

        // 把查询出来的结果重新组装成 Laravel-Admin 需要的格式
        $result->setCollection($result->getCollection()->map(function (\App\Models\Category $category) {
            return ['id' => $category->id, 'text' => $category->full_name];
        }));

        return $result;
    }

    // 获取子类
    public function apiChildren(Request $request)
    {
        // 用户输入的值通过 q 参数获取
        $search = $request->input('q');
        $categoryChildren = \App\Models\Category::where('parent_id', $search)->get();
        $options = [];
        foreach ($categoryChildren as $value) {
            $option = [
                'id'   => $value['id'],
                'text' => $value['name'],
            ];
            $options[] = $option;
        }
        return $options;
    }
}
